﻿// rim 外发光 菲涅尔效果
Shader "Demon/Demon_Rim/Rim"
{
	Properties
	{
		_MainTex ("Texture", 2D) = "white" {}
		_Fresnel("外发光强度(X)扩散度(Y)柔和度(Z)透明度(W)",vector) = (1,1,0,1)
		_FresnelColor("FresnelColor",Color) = (1,1,1,1)

		[Toggle]_REDUCE("reduce",int) = 0
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag
			// make fog work
			#pragma multi_compile_fog
			#pragma multi_compile _ _REDUCE_ON

			#include "UnityCG.cginc"

			struct appdata
			{
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
				float3 normal : NORMAL;
			};

			struct v2f
			{
				float2 uv : TEXCOORD0;
				UNITY_FOG_COORDS(1)
				float4 vertex : SV_POSITION;
				float3 worldNormal : TEXCOORD1;
				float3 worldPos	   : TEXCOORD2;
			};

			sampler2D _MainTex;
			float4 _MainTex_ST;
			float4 _Fresnel;
			float4 _FresnelColor;
			
			v2f vert (appdata v)
			{
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = TRANSFORM_TEX(v.uv, _MainTex);
				UNITY_TRANSFER_FOG(o,o.vertex);
				o.worldNormal = UnityObjectToWorldNormal(v.normal);
				o.worldPos = mul(unity_ObjectToWorld,v.vertex);
				return o;
			}
			
			fixed4 frag (v2f i) : SV_Target
			{
				//fixed4 col = tex2D(_MainTex, i.uv);
				float3 N = normalize(i.worldNormal);
				float3 V = normalize(_WorldSpaceCameraPos.xyz - i.worldPos);
				fixed4 fresnel;
				float NdotV = saturate(dot(N,V));
				#if _REDUCE_ON
					NdotV = 1 - NdotV;
				#endif
				fresnel = _Fresnel.x * pow(NdotV,_Fresnel.y);
				//fresnel = smoothstep(0.5,0.5+_Fresnel.z,fresnel); 
				fresnel *= _FresnelColor;
				fresnel *= _Fresnel.w;
				return fresnel;
			}
			ENDCG
		}
	}
}
